package model;


import java.util.TreeSet;

import view.PanelTeacherDetails;
import dataLayer.DAOAbsence;

public class Teacher implements Comparable<Teacher> {
	private static Teacher instance = null;
	private String Teacher_oldCode;
	private String Teacher_code;
	private String Teacher_name;

	private PanelTeacherDetails PTDform;

	private TreeSet<Absence> absenceList = new TreeSet<Absence>();
	private TreeSet<Absence> todayabsenceList = new TreeSet<Absence>();
	private TreeSet<Absence> weekabsenceList = new TreeSet<Absence>();
	private TreeSet<Absence> teacherAbsenceList = new TreeSet<Absence>();

	private TreeSet<Absence> absenties;

	public Teacher(String oldCode, String code, String name) {

		Teacher_oldCode = oldCode;
		Teacher_code = code;
		Teacher_name = name;
		//setAbsenties(new TreeSet<>());
		absenceList = DAOAbsence.getInstance().loadAllAbsence();
		todayabsenceList = DAOAbsence.getInstance().loadAllAbsenceToday();
		weekabsenceList = DAOAbsence.getInstance().loadAllAbsenceWeek();
		this.setPTDform(PTDform);

	}

	public PanelTeacherDetails getPTDform() {
		return PTDform;
	}

	public void setPTDform(PanelTeacherDetails pTDform) {
		PTDform = pTDform;
	}

	public TreeSet<Absence> getTodayabsenceList() {
		return todayabsenceList;
	}

	public void setTodayabsenceList() {
		Teacher.getInstance();
		//todayabsenceList = DAOAbsence.getInstance().loadAllAbsenceToday();
	}
	
	public TreeSet<Absence> getWeekabsenceList() {
		return weekabsenceList;
	}

	public void setWeekabsenceList() {
		Teacher.getInstance();
		//weekabsenceList = DAOAbsence.getInstance().loadAllAbsenceWeek();
	}

	public void Delete() {

	}

	public void addAbsence(Absence a) {
		absenceList.add(a);
	}

	public TreeSet<Absence> getAllAbsences() {
		return absenceList;
	}

	// TODO Get all Absences where startDay => date, and endDay =< date
	public TreeSet<Absence> getAllAbsencesToday() {

		return todayabsenceList;
	}
	
	// TODO Get all Absences where startDay => monday, and endDay =< sunday
		public TreeSet<Absence> getAllAbsencesWeek() {

			return weekabsenceList;
		}

	// Method to return the absence_Id from each Absence in treeset
	public int getAbsenceId(int absence) {
		for (Absence a : absenceList) {
			if (a.getAbsence_Id() == absence) {
				return a.getAbsence_Id();
			}
		}
		return 0;
	}

	// Method to return a Absence from the treeset
	public Absence getAbsence(Absence a) {
		int id = a.getAbsence_Id();
		for (Absence a1 : absenceList) {
			if (a1.getAbsence_Id() == id) {
				return a1;
			}
		}
		return null;
	}

	// Method to delete a Absence from the treeset
	public void deleteAbsence(Absence a) {
		int id = a.getAbsence_Id();
		for (Absence a2 : absenceList) {
			if (id == a2.getAbsence_Id()) {
				absenceList.remove(a2);
				break;
			}
		}
	}

	public TreeSet<Absence> getAllAbsencesFromTeacher(Teacher t) {
		teacherAbsenceList = DAOAbsence.getInstance().loadAbsenceFromTeacher(
				t);
		return teacherAbsenceList;

	}

	public String getTeacher_code() {
		return Teacher_code;
	}

	public void setTeacher_code(String teacher_code) {
		Teacher_code = teacher_code;
	}

	public String getTeacher_name() {
		return Teacher_name;
	}

	public String getTeacher_oldCode() {
		return Teacher_oldCode;
	}

	public void setTeacher_oldCode(String teacher_oldCode) {
		Teacher_oldCode = teacher_oldCode;
	}

	@Override
	public boolean equals(Object obj) {
		Teacher tempT = (Teacher) obj;
		return this.Teacher_code.equalsIgnoreCase(tempT.getTeacher_code());
	}

	@Override
	public int hashCode() {
		return Teacher_code.hashCode();
	}

	public void setTeacher_name(String teacher_name) {
		Teacher_name = teacher_name;
		Teacher_code.hashCode();
	}

	@Override
	public String toString() {
		return Teacher_oldCode + ": " + Teacher_code + ": " + Teacher_name;
	}

	// CompareTo for Teacher on teacher_code
	public int compareTo(Teacher t) {
		return Teacher_code.compareToIgnoreCase(t.getTeacher_code());
	}

	public static Teacher getInstance() {
		if (instance == null) {
			instance = new Teacher(null, null, null);
		}
		return instance;

	}

	

	public TreeSet<Absence> getAbsenties() {
		return absenties;
	}

	public void setAbsenties(TreeSet<Absence> absenties) {
		this.absenties = absenties;
	}

}
